package com.example.it.looker.model.datasource;

import com.example.it.looker.Global;
import com.example.it.looker.model.entity.News;
import com.example.it.looker.utils.DateUtil;
import com.example.it.looker.utils.L;
import com.example.it.looker.utils.NetUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 8/31/2017 11:00 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class NewsDataSource {


    public interface NewsListLoadedCallBack {
        /**
         * 回调函数，当获取新闻内容的时候调用
         *
         * @param newsList 新闻列表
         * @param topNews  热门新闻
         */
        void newsListLoadedCallBack(List<News> newsList, List<News> topNews);
    }


    private static NewsDataSource NEWS_DATA_SOURCE = null;

    /**
     * 加载往期信息时用到的标识
     */
    private static Date date;

    private NewsDataSource() {
    }

    /**
     * 获取唯一的实例
     */
    public static NewsDataSource getInstance() {
        if (NEWS_DATA_SOURCE == null) {
            NEWS_DATA_SOURCE = new NewsDataSource();
        }
        return NEWS_DATA_SOURCE;
    }

    /**
     * 获取最新的知乎日报新闻
     */
    public List<News> getLastNews(NewsListLoadedCallBack callBack) {
        String content = NetUtil.getRequest(Global.ZHIHU_LAST_NEWS_API);

        try {
            JSONObject jsonObject = new JSONObject(content);

            List<News> newsList = toNewsList(jsonObject.getJSONArray("stories"));
            List<News> topNews = topNews(jsonObject.getJSONArray("top_stories"));

            callBack.newsListLoadedCallBack(newsList, topNews);
        } catch (Exception e) {
            e.printStackTrace();
            L.i(this.getClass().getSimpleName() + ".getLastNews:Error");
        }
        return null;
    }

    /**
     * 加载往期的知乎日报
     */
    public List<News> LoadMore() {

        if (date == null) {
            date = new Date(System.currentTimeMillis());
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            date = calendar.getTime();
        }
        //减去一天的时间


        String strDate = DateUtil.format(date);
        String url = Global.getBeforeNewUrl(strDate);
        String jsonContent = NetUtil.getRequest(url);
        try {
            JSONObject jsonObject = new JSONObject(jsonContent);
            List<News> newsList = toNewsList(jsonObject.getJSONArray("stories"));

            return newsList;
        } catch (Exception e) {
            e.printStackTrace();
            L.i("NewsDataSource-LoadMore Error");
            return null;
        }
    }

    /**
     * 获取新闻列表
     */
    private List<News> toNewsList(JSONArray newsArray) {
        try {
            List<News> newsList = new ArrayList<>();
            try {
                for (int i = 0; i < newsArray.length(); i++) {

                    News news = toNews(newsArray.getJSONObject(i));
                    newsList.add(news);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return newsList;
        } catch (Exception e) {
            e.printStackTrace();
            L.i(this.getClass().getSimpleName() + ".toNews: Error");
            return null;
        }
    }

    /**
     * 获取最新的热门内容
     */
    private List<News> topNews(JSONArray topNewsArray) {
        try {
            List<News> newsList = new ArrayList<>();
            try {
                for (int i = 0; i < topNewsArray.length(); i++) {

                    News news = toNews(topNewsArray.getJSONObject(i));
                    newsList.add(news);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return newsList;
        } catch (Exception e) {
            e.printStackTrace();
            L.i(this.getClass().getSimpleName() + ".toNews: Error");
            return null;
        }
    }

    /**
     * 将JSONObject转换为News对象
     */
    private News toNews(JSONObject jsonObject) throws Exception {
        String id = jsonObject.getString("id");
        String title = jsonObject.getString("title");
        String imgUrl = null;
        try {
            imgUrl = jsonObject.getJSONArray("images").getString(0);
        } catch (Exception e) {
            imgUrl = jsonObject.getString("image");
        }
        String time = "今天";

        News news = new News(id, imgUrl, title, time);
        return news;
    }


}
